package com.stedu.mall.userservice.controller;

import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageInfo;
import com.stedu.mall.common.bean.RespBean;
import com.stedu.mall.common.bean.User;
import com.stedu.mall.common.exception.SteduException;
import com.stedu.mall.common.service.UserService;
import com.stedu.mall.common.uitl.JwtUtil;
import com.stedu.mall.common.uitl.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private UserService userService;

    @GetMapping("/captcha")
    public RespBean captcha(){
        //创建验证码对象
        LineCaptcha captcha = new LineCaptcha(120,32,4,20);
        //生成唯一的key
        String key = IdUtil.getSnowflakeNextIdStr();
        //将key和验证码文本保存到Redis中
        redisUtil.set(key,captcha.getCode(),60);
        //将验证码图片转换成Base64编码
        String imageBase64Data = captcha.getImageBase64Data();
        //将key和Base64编码保存在map中
        HashMap<String,Object> map = new HashMap<>();
        map.put("key",key);
        map.put("imageBase64Data", imageBase64Data);
        //将key和Base64图片返回
        return RespBean.ok("生成验证码成功",map);
    }

    @PostMapping("/login")
    public RespBean login(String username,String password,String key,String captchaInput) throws SteduException {
        //判断验证码是否正确 - 从Redis中查询验证码判断
        String captcha = (String)redisUtil.get(key);
        if(captcha == null || !captcha.equalsIgnoreCase(captchaInput)){
            throw new SteduException("验证码错误，请确认后重新输入");
        }
        //登录
        User user = userService.login(username, password);
        //生成token
        HashMap<String,Object>map = new HashMap<>();
        map.put("id", user.getId());
        map.put("username", user.getUsername());
        map.put("role", "user");
        String jwtStr = JwtUtil.generateJwt(map);
        //将token返回给用户
        return RespBean.ok("登陆成功", jwtStr);
    }
    @GetMapping("/info")
    public RespBean getInfo(@RequestHeader("token") String token) {
        Map<String,Object> map = JwtUtil.parseJwtToMap(token);
        Integer id = (Integer) map.get("id");
        User user = userService.selectById(id);
        //返回
        return  RespBean.ok("",user);
    }

    @PostMapping("/reg")
    public RespBean reg(String username, String password, String key, String captchaInput) throws SteduException {
        String captcha = (String) redisUtil.get(key);
        if(captcha == null || !captcha.equalsIgnoreCase(captchaInput)){
            throw new SteduException("验证码错误，请确认后重新输入");
        }
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        //注册
        userService.reg(user);

        return RespBean.ok("注册成功");
    }

    @GetMapping("/selectAll")
    public RespBean selectAll(User condition, Integer pageNum, Integer pageSize){
       /* pageNum = pageNum == null?1:pageNum;
        pageSize = pageSize == null ? 5:pageSize;*/
        PageInfo<User> pageInfo = userService.selectAll(condition,pageNum,pageSize);
        return RespBean.ok("查询成功",pageInfo);
    }
    @DeleteMapping("/{id}")
    public RespBean delete(@PathVariable("id") Integer id){
        if(userService.selectById(id).getStatus()==0){
            userService.delete(id);
            return RespBean.ok("禁用成功");
        }else {
            userService.delete(id);
            return RespBean.ok("启用成功");
        }
    }
    @PutMapping("/reset/{id}")
    public RespBean reset(@PathVariable("id") Integer id){
        User user = userService.selectById(id);
        userService.update(user);
        return RespBean.ok("重置密码成功");
    }
    @PostMapping("/insert")
    public RespBean inset(@RequestBody User user)  {
        userService.insert(user);
        return RespBean.ok("添加成功");
    }
    @PutMapping("/updateStatus")
    public RespBean update(@RequestBody User user){
        userService.updateStatus(user);
        return RespBean.ok("切换状态成功");
    }

    @PutMapping("/updateInfo")
    public RespBean updateInfo(@RequestBody User user, @RequestHeader("token") String token) {
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer id = (Integer) map.get("id");
        user.setId(id);
        userService.updateInfo(user);
        return RespBean.ok("修改成功");
    }

    //修改密码
    @PutMapping("/updatePassword")
    public RespBean updatePassword(String oldPassword, String newPassword1, String newPassword2, @RequestHeader("token") String token) throws SteduException {
        if (!newPassword1.equals(newPassword2)) {
            throw new SteduException("两次输入的新密码不一致，无法修改");
        }
        //解析token
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        //获取用户id - 根据id查询用户信息
        Integer id = (Integer) map.get("id");

        userService.updatePassword(oldPassword, newPassword1, id);

        return RespBean.ok("修改密码成功，请重新登录");
    }

    @PutMapping("/updatePayPassword1")
    public RespBean updatePayPassword1(String oldPayPassword, String newPayPassword1, String newPayPassword2, @RequestHeader("token") String token) throws SteduException {
        if (!newPayPassword1.equals(newPayPassword2)) {
            throw new SteduException("两次输入的新密码不一致，无法修改");
        }
        //解析token
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        //获取用户id - 根据id查询用户信息
        Integer id = (Integer) map.get("id");

        userService.updatePayPassword1(oldPayPassword, newPayPassword1, id);

        return RespBean.ok("修改密码成功，请重新登录");
    }

    @PutMapping("/updatePayPassword2")
    public RespBean updatePayPassword2(String newPayPassword1, String newPayPassword2, @RequestHeader("token") String token) throws SteduException {
        if (!newPayPassword1.equals(newPayPassword2)) {
            throw new SteduException("两次输入的新密码不一致，无法修改");
        }
        //解析token
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        //获取用户id - 根据id查询用户信息
        Integer id = (Integer) map.get("id");

        userService.updatePayPassword2(newPayPassword1, id);

        return RespBean.ok("修改密码成功");
    }
    @GetMapping("/{id}")
    public RespBean selectById(@PathVariable("id") Integer id){
        User user = userService.selectById(id);
        return RespBean.ok("",user);
    }

    @PutMapping("/recharge/{money}")
    public RespBean recharge(@RequestHeader("token") String token, @PathVariable("money") BigDecimal money) {
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer id = (Integer) map.get("id");
        User user = new User();
        user.setId(id);
        userService.recharge(user, money);
        return RespBean.ok("充值成功");
    }
}

